// 图表文字自适应
const FontSize:any  =  (res:number) => {
	let clientWidth:number = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	if (!clientWidth) return;
	if(clientWidth<1200){
		clientWidth = 1200
	}
	let fontSize = res/(75/(clientWidth/18))
	return fontSize	
}
// 设置 rem 函数
function setRem () {

    // 320 默认大小16px; 320px = 20rem ;每个元素px基础上/16
    let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
//得到html的Dom元素
    let htmlDom = document.getElementsByTagName('html')[0];
//设置根元素字体大小
    htmlDom.style.fontSize= htmlWidth/10 + 'px';
}
function ArrayDim (dimension: number, initial: number){
	var a = [],i;
	for(i=0;i < dimension; i++){
		a[i] = initial;
	}
	return a;
};
// 防抖
let timer = null as any
function  debounce(fn:any, delay:number) {
    // 将debounce处理结果当作函数返回
    return function (this: any){
        // 保留调用时的this上下文
        let context = this
        // 保留调用时传入的参数
        let args = arguments
 
        // 每次事件被触发时，都去清除之前的旧定时器
		console.log(timer)
        if(timer) {
            clearTimeout(timer)
        }
        // 设立新定时器
        timer = setTimeout(function () {
            fn.apply(context, args)
        }, delay)
    }
}
// 没有图片返回默认图片
function defaluteImg(url:string|undefined){
    if(!url){
		return require('../assets/image/defalut.png')
	}else{
		return url
	}
}
// 对比两段文字相似度
function similar(s:string, t:string, f:any) {
	return new Promise((resolve, reject) => {
		if (!s || !t) {
		  return 0
		}
		if(s === t){
		  return 100;
		}
		var l = s.length > t.length ? s.length : t.length
		var n = s.length
		var m = t.length
		var d = []
		f = f || 2
		var min = function (a:any, b:any, c:any) {
		  return a < b ? (a < c ? a : c) : (b < c ? b : c)
		}
		var i, j, si, tj, cost
		if (n === 0) return m
		if (m === 0) return n
		for (i = 0; i <= n; i++) {
		  d[i] = [] as any
		  d[i][0] = i
		}
		for (j = 0; j <= m; j++) {
		  d[0][j] = j
		}
		for (i = 1; i <= n; i++) {
		  si = s.charAt(i - 1)
		  for (j = 1; j <= m; j++) {
		    tj = t.charAt(j - 1)
		    if (si === tj) {
		      cost = 0
		    } else {
		      cost = 1
		    }
		    d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost)
		  }
		}
		let res = (1 - d[n][m] / l) *100
		resolve(res.toFixed(f))
	})
}
function gradeTotal(grade:any){
	console.log(grade)
	grade = Number(grade)
    let name = ""
	if(grade < 50){
		name = '若禺'
	}else if(grade > 50 && grade <= 100){
		name = '守拙'
	}else if(grade > 100 && grade <= 200){
		name = '斗力'
	}else if(grade > 200 && grade <= 400){
		name = '小巧'
	}else if(grade > 400 && grade <= 600){
		name = '用智'
	}else if(grade > 600 && grade <= 700){
		name = '通幽'
	}else if(grade > 700 && grade <= 800){
		name = '俱体'
	}else if(grade > 800 && grade <= 1000){
		name = '坐照'
	}else if(grade > 1000 && grade <= 1200){
		name = '入神'
	}else if(grade > 1200){
		name = '棋圣'
	}
	  
	return name
}

function GameTimeMain(this:any){
	
	// 计时器
	this.intervalTime = null as any
	this.gameTime = '' as string
	this.gameText = '' as string
	
	this.stop = (callback:any)=>{
		if(this.intervalTime){
			this.gameTime = '00:00:00'
			this.gameText = ''
			clearInterval(this.intervalTime)
			this.intervalTime = null
			callback({
				gameTime:this.gameTime,
				gameText:this.gameText
			})
		}
	}
	this.start = (callback:any)=>{
		this.stop()
		this.h = 0 as number
		this.m = 0 as number
		this.s = 0 as number
		this.st = 0 as string | number
		this.mt = 0 as string | number
		this.ht = 0 as string | number
		this.intervalTime = setInterval(()=>{
			this.s++
			if(this.s === 60){
				this.m = this.m + 1
				this.s = 0
			}
			if(this.m === 60){
				this.h = this.h + 1
				this.m = 0
			}
			this.st = this.s < 10?'0'+this.s:this.s
			this.mt = this.m < 10?'0'+this.m:this.m
			this.ht = this.h < 10?'0'+this.h:this.h
			this.gameTime =  this.ht +':'+ this.mt + ':' + this.st
			this.gameText = this.h+'时'+ this.m +'分'+this.s+'秒'
			callback({
				gameTime:this.gameTime,
				gameText:this.gameText
			})
		},1000)
	}	
}
let gameTimeMain = new (GameTimeMain as any)()
let session = {
	set:(key:string,value:any)=>{
		localStorage.setItem(key,value)
		return true
	},
	get:(key:string)=>{
		return localStorage.getItem(key)
	},
	delete:(key:string)=>{
		localStorage.removeItem(key)
		return true
	},
}
const cachLoading = (key:string,value?:any)=>{
	let isUpdate = false
	if(session.get(key)){
		let result = JSON.parse(session.get(key) as any)
		isUpdate =  result
		if(value && (result[0].id !== value[0].id)){
			session.set(key,JSON.stringify(value))
		}
	}else{
		if(value){
			session.set(key,JSON.stringify(value))
		}
	}
	return isUpdate
}

export {FontSize, ArrayDim, setRem,debounce,defaluteImg,similar,gradeTotal,gameTimeMain,cachLoading,session}